#include "../header/common.h"
#define ANZAHLOPTION 6

int options_maus_klick(int x,int y,SDL_Rect dist[ANZAHLOPTION],SDL_Surface *screen)
{
  int i,resolution,sound;
  options(1,&sound,&resolution);
  for (i=0;i<ANZAHLOPTION;i++)
    {
      if((x > dist[i].x) && (x < dist[i].x + dist[i].w) && (y > dist[i].y) && (y < dist[i].y + dist[i].h))
	{
	  switch(i)
	    {
	    case 0:
	      sound=1;
	      play(2);
	      options(0,&sound,&resolution);  
	      break;
	    case 1:
	      sound=0;
	      play(0);
	      options(0,&sound,&resolution);  
	      break;
	    case 2: 
	      resolution=1;
	      options(0,&sound,&resolution);  
	      return 2;
	    case 3:
	      resolution=2;
	      options(0,&sound,&resolution);
	      return 2;  
	      break;
	    case 4:
	      resolution=3;
	      options(0,&sound,&resolution);
	      return 2;  
	      break;
	    case 5:
	      return 3;
	    default: break;			
	    }
	}
    }
  return 0;
}

int options_menu(SDL_Surface *screen)
{
  int resolution,sound;
  SDL_Surface *image[ANZAHLOPTION],*imagepressed[ANZAHLOPTION],*background,*backmenus,*temp;
  SDL_Rect dst[ANZAHLOPTION],backmenu;
  SDL_Event event;
  char bmp_filesmall[] = "image/640480/buttons/option#.bmp";
  char bmp_filemedium[] = "image/800600/buttons/option#.bmp";
  char bmp_filebig[] = "image/1024768/buttons/option#.bmp";
  char bmp_filesmall_pressed[] = "image/640480/buttonspressed/aoption#.bmp";
  char bmp_filemedium_pressed[] = "image/800600/buttonspressed/aoption#.bmp";
  char bmp_filebig_pressed[] = "image/1024768/buttonspressed/aoption#.bmp";
  int done = 0, i, x, y;
  Uint8 SDL_GetRelativeMouseState(int *x, int *y);
  if(screen->w==640)
    {
      background = SDL_LoadBMP("image/640480/menubackground.bmp");
      backmenus = SDL_LoadBMP("image/640480/options.bmp");
      temp=SDL_LoadBMP("image/640480/temp.bmp"); 
    }
  if(screen->w==800)
    {
      background = SDL_LoadBMP("image/800600/menubackground.bmp");
      backmenus = SDL_LoadBMP("image/800600/options.bmp");
      temp=SDL_LoadBMP("image/800600/temp.bmp");
    }
  if(screen->w==1024)
    {
      background = SDL_LoadBMP("image/1024768/menubackground.bmp");
      backmenus = SDL_LoadBMP("image/1024768/options.bmp");
      temp=SDL_LoadBMP("image/1024768/temp.bmp");
    }			   
  zeige_maus();

  for(i=0;i<2;i++)//sound
    {
      if(screen->w==640)
	{
	  bmp_filesmall[27] = i+48;image[i] = SDL_LoadBMP(bmp_filesmall);
	  bmp_filesmall_pressed[35] = i+48;imagepressed[i] = SDL_LoadBMP(bmp_filesmall_pressed);
	}
      if(screen->w==800)
	{
	  bmp_filemedium[27] = i+48;image[i] = SDL_LoadBMP(bmp_filemedium);
	  bmp_filemedium_pressed[35] = i+48;imagepressed[i] = SDL_LoadBMP(bmp_filemedium_pressed);	  
	}
      if(screen->w==1024)
	{
	  bmp_filebig[28] = i+48;image[i] = SDL_LoadBMP(bmp_filebig);
	  bmp_filebig_pressed[36] = i+48;imagepressed[i] = SDL_LoadBMP(bmp_filebig_pressed);
	}
      dst[i].x = ((screen->w/2)-(image[i]->w/2))+image[i]->w*i;
      dst[i].y = ((screen->h/100)*36);    	
      dst[i].w = image[i]->w*i;
      dst[i].h = image[i]->h*i;
      SDL_SetColorKey(imagepressed[i], SDL_SRCCOLORKEY, SDL_MapRGB(imagepressed[i]->format, 0, 0, 255)); 
      SDL_SetColorKey(image[i], SDL_SRCCOLORKEY, SDL_MapRGB(image[i]->format, 0, 0, 255));
      options(1,&sound,&resolution); 
      if(sound)SDL_BlitSurface(imagepressed[0],NULL,image[0],NULL);else SDL_BlitSurface(imagepressed[1],NULL,image[1],NULL);
      SDL_BlitSurface(image[i], NULL, backmenus, &dst[i]); 
    }
  for(i=2;i<5;i++)//auflösung
    {
      if(screen->w==640)
	{
	  bmp_filesmall[27] = i+48;image[i] = SDL_LoadBMP(bmp_filesmall);
	  bmp_filesmall_pressed[35] = i+48;imagepressed[i] = SDL_LoadBMP(bmp_filesmall_pressed);
	}
      if(screen->w==800)
	{
	  bmp_filemedium[27] = i+48;image[i] = SDL_LoadBMP(bmp_filemedium);
	  bmp_filemedium_pressed[35] = i+48;imagepressed[i] = SDL_LoadBMP(bmp_filemedium_pressed);	  
	}
      if(screen->w==1024)
	{
	  bmp_filebig[28] = i+48;image[i] = SDL_LoadBMP(bmp_filebig);
	  bmp_filebig_pressed[36] = i+48;imagepressed[i] = SDL_LoadBMP(bmp_filebig_pressed);
	}
      dst[i].x = ((screen->w/2)-(image[i]->w/2))+image[i]->w*(i-2);
      dst[i].y = ((screen->h/100)*53);    	
      dst[i].w = (image[i]->w*i);
      dst[i].h = image[i]->h*i;
      SDL_SetColorKey(imagepressed[i], SDL_SRCCOLORKEY, SDL_MapRGB(imagepressed[i]->format, 0, 0, 255)); 
      SDL_SetColorKey(image[i], SDL_SRCCOLORKEY, SDL_MapRGB(image[i]->format, 0, 0, 255)); 
      switch(resolution)
	{
	case 1:
	  SDL_BlitSurface(imagepressed[2],NULL,image[2],NULL);
	  break;
	case 2:
	  SDL_BlitSurface(imagepressed[3],NULL,image[3],NULL);
	  break;
	case 3:
	  SDL_BlitSurface(imagepressed[4],NULL,image[4],NULL);
	  break;
	}
      SDL_BlitSurface(image[i], NULL, backmenus, &dst[i]); 
    }
  
  for(i=5;i<6;i++)//back
    {
      if(screen->w==640)
	{
	  bmp_filesmall[27] = i+48;image[i] = SDL_LoadBMP(bmp_filesmall);
	  bmp_filesmall_pressed[35] = i+48;imagepressed[i] = SDL_LoadBMP(bmp_filesmall_pressed);
	}
      if(screen->w==800)
	{
	  bmp_filemedium[27] = i+48;image[i] = SDL_LoadBMP(bmp_filemedium);
	  bmp_filemedium_pressed[35] = i+48;imagepressed[i] = SDL_LoadBMP(bmp_filemedium_pressed);	  
	}
      if(screen->w==1024)
	{
	  bmp_filebig[28] = i+48;image[i] = SDL_LoadBMP(bmp_filebig);
	  bmp_filebig_pressed[36] = i+48;imagepressed[i] = SDL_LoadBMP(bmp_filebig_pressed);
	}
      dst[i].x = ((screen->w/4)-(image[i]->w/2));
      dst[i].y = ((screen->h/100)*70);    	
      dst[i].w = image[i]->w*i;
      dst[i].h = image[i]->h*i;
      SDL_SetColorKey(imagepressed[i], SDL_SRCCOLORKEY, SDL_MapRGB(imagepressed[i]->format, 0, 0, 255)); 
      SDL_SetColorKey(image[i], SDL_SRCCOLORKEY, SDL_MapRGB(image[i]->format, 0, 0, 255)); 
      SDL_BlitSurface(image[i], NULL, backmenus, &dst[i]); 
    }
  
  SDL_SetColorKey(backmenus, SDL_SRCCOLORKEY, SDL_MapRGB(backmenus->format, 0, 0, 255));
  SDL_BlitSurface(background,NULL,screen,NULL);
  SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, 200);
  SDL_BlitSurface(temp,NULL,screen,NULL); 
  SDL_BlitSurface(backmenus,NULL,screen,NULL);
  SDL_Flip(screen);
  
while (!done) 
  {
    while (SDL_PollEvent(&event)) 
      {
	/* Mausklick abfangen */
	SDL_PumpEvents();
	if(SDL_GetMouseState(&x, &y) &SDL_BUTTON(1))
	  {
	    done = options_maus_klick(x,y,dst,screen);
	  }
	if(!done)
	  {
	    SDL_Delay(15);
	    for(i=0;i<ANZAHLOPTION;i++)
	      {
		if(click_button(dst[i],x,y))
		  {
		    SDL_BlitSurface(imagepressed[i], NULL, screen, &dst[i]);
		    SDL_Flip(screen);
		  }
		else
		  {
		    SDL_BlitSurface(image[i],NULL,screen, &dst[i]);
		    SDL_Flip(screen);
		  }	
	      }
	  }
 	switch (event.type) 
 	  { 
 	  case SDL_QUIT: 
	    for(i=0;i<ANZAHLOPTION;i++)
	      {
		SDL_FreeSurface(image[i]);
		SDL_FreeSurface(imagepressed[i]);
	      } 
	    SDL_FreeSurface(backmenus);
	    SDL_FreeSurface(temp);
 	    return 1; 
 	    break; 
 	  } 
      }
  }
for(i=0;i<ANZAHLOPTION;i++)
  {
    SDL_FreeSurface(image[i]);
    SDL_FreeSurface(imagepressed[i]);
  } 
SDL_FreeSurface(backmenus);
SDL_FreeSurface(temp);
return(done);
}

